package cn.tedu._06artivle.base.exception;

import cn.tedu._06artivle.base.response.JsonResult;
import cn.tedu._06artivle.base.response.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(RuntimeException.class)
    public JsonResult runtimeException(RuntimeException e) {
        String msg = e.getMessage();
        log.error(msg, e);
        return new JsonResult(StatusCode.NOT_LOGIN, msg);
    }

    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult methodArgumentNotValidException(MethodArgumentNotValidException e) {
        String msg = e.getMessage();
        log.error(msg, e);
        return new JsonResult(StatusCode.VALIDATED_ERROR, msg);
    }
}
